﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime;
using System.Runtime.Serialization;
using GeneratorLib.DB.Schemas;
using Generator.DBUtility;

namespace GeneratorLib.DB.Connectios
{

    [Serializable]
    public class MYSQLConnection : DBConnectionBase, ISerializable
    {
        public override string DBTypeMapping
        {
            get
            {
                return "MySQLDbType";
            }
        }

        //DBParameter------------------------------------------------------
        public override string DbParamType
        {
            get
            {
                return "MySqlParameter";
            }
        }
        public override string DbParamDbType
        {
            get
            {
                return "MySqlDbType";
            }
        }
        public override string PreParameter
        {
            get
            {
                return "?";
            }
        }
        //DBParameter------------------------------------------------------

        public MYSQLConnection(string name, string connectStr):base(name,connectStr)
        {
        }

        public MYSQLConnection(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        public override IDBProvider DBProvider
        {
            get
            {
                return null;
            }
        }

        public override List<FieldInfo> GetTableField(TableInfo table)
        {
            DataSet dtSet = MYSQLUtility.GetTableFields(this.ConnectStr,table.TableName);
            List<FieldInfo> fields = new List<FieldInfo>();

            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                FieldInfo field = new FieldInfo();
                field.Table = table;
                field.FieldName = string.Format("{0}", row["Field"]);
                field.SetFieldType(string.Format("{0}", row["Type"]));
                if (string.Format("{0}", row["Null"]).ToLower().CompareTo("yes") == 0)
                    field.Nullabled = true;
                else
                    field.Nullabled = false;
                string key = string.Format("{0}", row["Key"]);
                if (key.ToLower().CompareTo("pri") == 0)
                    field.Key = true;
                else
                    field.Key = false;
                field.DefaultValue = string.Format("{0}", row["Default"]);
                field.Comment = string.Format("{0}", row["Comment"]);
                fields.Add( field);
            }
            return fields;
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }

       

        protected override void FillTables()
        {
            tables = new List<TableInfo>();
            DataSet dtSet = null;
            try
            {
                dtSet = MYSQLUtility.GetTableNames(this.ConnectStr);
            }
            catch {
                return;
            }

            if (dtSet == null) return;

            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                TableInfo table = new TableInfo(this, string.Format("{0}", row[0]));
                tables.Add(table);
            }
        }

        protected override void FillTableAddins()
        {
            tableAddins = new List<TableInfoAddin>();
            DataSet dtSet = null;
            try
            {
                dtSet = MYSQLUtility.GetTableNames(this.ConnectStr);
            }
            catch
            {
                return;
            }
            
            if (dtSet == null) return;
            
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                TableInfoAddin tableAddin = new TableInfoAddin(string.Format("{0}", row[0]));
                tableAddins.Add(tableAddin);
            }
        }

    }








}
